<?php
if ( IPB_THIS_SCRIPT != 'admin' )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'init.php'.";
	exit();
}

class ad_forums
{
	var $ipsclass;
	var $admin_skin;
	var $need_desc;
	
	function auto_run()
	{
		$this->ipsclass->forums->forums_init();
		
		$code_redirect = array(
								'list' => 'list_forums',
								'save' => 'save_forum',
							);
		if(!$code_redirect[$this->ipsclass->input['do']])
		{
			$this->ipsclass->input['do'] = 'list';
		}
		$run = $code_redirect[$this->ipsclass->input['do']];
		$this->$run();		
	}
	
	function list_forums()
	{
		$this->admin_skin = new admin_skin();
		$this->admin_skin->ipsclass =& $this->ipsclass;
		$this->admin_skin->init_admin_skin();
		//$this->ipsclass->input['pforum'] = intval($this->ipsclass->input['pforum']);
		
		$this->ipsclass->admin->page_title  = "Welcome to managing the forums involving ibMarket";
		$this->ipsclass->admin->page_detail = "Welcome to managing the forums involving ibMarket. Here you can edit specific settings to a single forum.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=forum', 'Manage Forums' );
		
		$need_desc = array();
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array(
																	 	  	 1 => array( 'section'   , 'components'  ),
																			 2 => array( 'act'       , 'market'      ),
																			 3 => array( 'code'      , 'forum'       ),
																			 4 => array( 'do'        , 'save'        ),
																	)		);
		
		foreach( $this->ipsclass->forums->forum_cache['root'] as $forum_data )
		{
			$this->ipsclass->adskin->td_header[] = array( "" , "100%" );
			
			$this->ipsclass->html .= $this->ipsclass->adskin->start_table($forum_data['name']);
			
			$this->output_subforums($forum_data['id']);
			
			$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		}
		
        if ( $this->ipsclass->vars['forum_cache_minimum'] and count($this->need_desc) )
        {
        	$this->ipsclass->DB->simple_construct( array( 'select' => 'id,description', 'from' => 'forums', 'where' => 'id IN('.implode( ',', $need_desc ) .')' ) );
        	$this->ipsclass->DB->simple_exec();
        	
        	while( $r = $this->ipsclass->DB->fetch_row() )
        	{
        		$this->ipsclass->html = str_replace( "<!--DESCRIPTION:{$r['id']}-->", $r['description'], $this->ipsclass->html );
        	}
        }
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form_standalone("Save Forum Settings");
		
		$this->ipsclass->admin->output();
	}
	
	function output_subforums($forum_id,$level = 0)
	{
		if ( is_array( $this->ipsclass->forums->forum_cache[ $forum_id ] ) )
		{
			foreach( $this->ipsclass->forums->forum_cache[ $forum_id ] as $forum_data )
			{
				if ( $this->ipsclass->vars['forum_cache_minimum'] )
				{
					$forum_data['description'] = "<!--DESCRIPTION:{$forum_data['id']}-->";
					$this->need_desc[]         = $forum_data['id'];
				}
				
				$html = "<b>".str_repeat($this->ipsclass->forums->depth_guide,$level).($level ? ' ' : '' )."<a href='#' onclick='toggleview(\"forum_".$forum_data['id']."\")'>".$forum_data['name']."</a></b><br />".$forum_data['description'];
				$html .= "<div id='forum_".$forum_data['id']."' style='display:".($forum_data['market_reply'] || $forum_data['market_topic'] ? '' : 'none')."'><br />";
				$this->admin_skin->td_header[] = array( "" , "60%" );
				$this->admin_skin->td_header[] = array( "" , "40%" );
				$html .= $this->admin_skin->start_table($this->ipsclass->forums->forum_by_id[$this->ipsclass->input['forum']]['name'].' Settings');
				$html .= $this->admin_skin->add_td_row(array(
																"Should this forum have different money gained per topic reply than the global setting?",
																$this->admin_skin->form_yes_no('forum['.$forum_data['id'].'][market_reply]',$forum_data['market_reply']),
														)	);
				$html .= $this->admin_skin->add_td_row(array(
																"If so, amount of to gain per topic reply?",
																$this->admin_skin->form_simple_input('forum['.$forum_data['id'].'][market_reply_amount]',$forum_data['market_reply_amount']),
														)	);
				$html .= $this->admin_skin->add_td_row(array(
																"Should this forum have different money gained per new topic than the global setting?",
																$this->admin_skin->form_yes_no('forum['.$forum_data['id'].'][market_topic]',$forum_data['market_topic']),
														)	);
				$html .= $this->admin_skin->add_td_row(array(
																"If so, amount of to gain per new topic?",
																$this->admin_skin->form_simple_input('forum['.$forum_data['id'].'][market_topic_amount]',$forum_data['market_topic_amount']),
														)	);
				$html .= $this->admin_skin->end_table();
				$html .= "</div>";
				
				$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic($html);
				
				if ( is_array( $this->ipsclass->forums->forum_cache[ $forum_data['id'] ] ) )
				{
					$this->output_subforums($forum_data['id'],$level+1);
				}
			}
		}
		else
		{
			if($level == 0)
			{
				$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("No Forums");
			}
		}
	}
	
	function save_forum()
	{
		if(!is_array($this->ipsclass->input['forum']))
		{
			$this->ipsclass->admin->error("Invalid input");
		}
		
		foreach($this->ipsclass->input['forum'] as $id => $input)
		{
			if(!$input || !is_array($this->ipsclass->forums->forum_by_id[$id]))
			{
				continue;
			}
			
			$this->ipsclass->DB->do_update('forums',array(
																'market_reply' => $input['market_reply'] & 1,
																'market_reply_amount' => $this->ipsclass->numbers->parse_input_number($input['market_reply_amount'],'int'),
																'market_topic' => $input['market_topic'] & 1,
																'market_topic_amount' => $this->ipsclass->numbers->parse_input_number($input['market_topic_amount'],'int'),
											),"id=".$id);
		}
		
		$this->ipsclass->update_forum_cache();
		
		$this->ipsclass->admin->save_log("ibMarket: Forum settings updated");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=forum","Forums edited");
	}
}
?>